package org.chen.ctrip.rmq.domain;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.UUID;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class AiProcessingMessage {
    private String productId;
    private String source; // 来源：CRAWL, MANUAL, BATCH, RETRY等
    private LocalDateTime createTime;
    private int retryCount;
    private String traceId; // 链路追踪ID
    
    public AiProcessingMessage(String productId, String source) {
        this.productId = productId;
        this.source = source;
        this.createTime = LocalDateTime.now();
        this.retryCount = 0;
        this.traceId = UUID.randomUUID().toString();
    }
    
    public String toJson() {
        try {
            return new ObjectMapper().writeValueAsString(this);
        } catch (Exception e) {
            return String.format("{\"productId\":\"%s\",\"source\":\"%s\"}", productId, source);
        }
    }
    
    public static AiProcessingMessage fromJson(String json) {
        try {
            return new ObjectMapper().readValue(json, AiProcessingMessage.class);
        } catch (Exception e) {
            log.error("解析AI处理消息失败: {}", json, e);
            return null;
        }
    }
}